package com.wei.czz.framework.game.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wei.czz.common.dto.game.GameFlowDto;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.vo.game.GameFlowFormVo;
import com.wei.czz.framework.admin.service.UserService;
import com.wei.czz.framework.game.service.GamePrizeService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wei.czz.framework.game.dao.GameFlowDao;
import com.wei.czz.framework.game.entity.GameFlowEntity;
import com.wei.czz.framework.game.service.GameFlowService;
import org.springframework.util.CollectionUtils;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2023-03-23 16:09:31
 * className: GameFlowServiceImpl
 * version: 1.0
 * description:
 */
@Service("gameFlowService")
@AllArgsConstructor
public class GameFlowServiceImpl extends ServiceImpl<GameFlowDao, GameFlowEntity> implements GameFlowService {

    private static final Logger log = LoggerFactory.getLogger(GameFlowServiceImpl.class);

    private final GamePrizeService gamePrizeService;

    private final UserService userService;

    @Override
    public void saveGameFlow(Long gameId, Long userId, Long prizeId, Long finalPrizeId) {
        GameFlowEntity gameFlow = new GameFlowEntity();
        gameFlow.setGameId(gameId)
                .setUserId(userId)
                .setPrizeId(prizeId)
                .setFinalPrizeId(finalPrizeId)
                .setCreateTime(new Date());
        // 插入数据
        baseMapper.insert(gameFlow);
        log.info("游戏流水插入完成。{}", gameFlow);
    }

    @Override
    public PageDto<GameFlowDto> pageGameFlowList(GameFlowFormVo gameFlowFormVo) {

        Page<GameFlowEntity> page = new Page<>(gameFlowFormVo.getPage(), gameFlowFormVo.getLimit());

        LambdaQueryWrapper<GameFlowEntity> gameFlowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 查询字段
        gameFlowLambdaQueryWrapper.select(GameFlowEntity::getId, GameFlowEntity::getUserId, GameFlowEntity::getPrizeId,
                GameFlowEntity::getFinalPrizeId, GameFlowEntity::getCreateTime);
        // 匹配条件
        gameFlowLambdaQueryWrapper.eq(GameFlowEntity::getGameId, gameFlowFormVo.getGameId())
                .eq(Objects.nonNull(gameFlowFormVo.getUserId()), GameFlowEntity::getUserId, gameFlowFormVo.getUserId())
                .eq(Objects.nonNull(gameFlowFormVo.getPrizeId()), GameFlowEntity::getFinalPrizeId, gameFlowFormVo.getPrizeId())
                .ge(Objects.nonNull(gameFlowFormVo.getStartTime()), GameFlowEntity::getCreateTime, gameFlowFormVo.getStartTime())
                .le(Objects.nonNull(gameFlowFormVo.getEndTime()), GameFlowEntity::getCreateTime, gameFlowFormVo.getEndTime());
        // 查询数据
        baseMapper.selectPage(page, gameFlowLambdaQueryWrapper);
        // 数据封装
        List<GameFlowEntity> gameFlowList = page.getRecords();
        if (gameFlowList.isEmpty()) {
            return new PageDto<>(Collections.emptyList(), page.getTotal(), gameFlowFormVo.getPage(), gameFlowFormVo.getLimit());
        }

        Set<Long> prizeIdSet = new HashSet<>();
        Set<Long> userIdSet = new HashSet<>();
        for (GameFlowEntity gameFlow : gameFlowList) {
            prizeIdSet.add(gameFlow.getPrizeId());
            prizeIdSet.add(gameFlow.getFinalPrizeId());
            userIdSet.add(gameFlow.getUserId());
        }

        Map<Long, String> prizeNameMap = gamePrizeService.getPrizeNameMap(prizeIdSet);
        Map<Long, String> userNameMap = userService.getNameMap(userIdSet, Function.identity());

        List<GameFlowDto> gameFlowDtoList = new ArrayList<>(gameFlowList.size());
        for (GameFlowEntity gameFlow : gameFlowList) {
            GameFlowDto gameFlowDto = new GameFlowDto();
            gameFlowDto.setId(gameFlow.getId().toString())
                    .setUsername(userNameMap.get(gameFlow.getUserId()))
                    .setPrizeName(prizeNameMap.get(gameFlow.getPrizeId()))
                    .setFinalPrizeName(prizeNameMap.get(gameFlow.getFinalPrizeId()))
                    .setCreateTime(gameFlow.getCreateTime());
            gameFlowDtoList.add(gameFlowDto);
        }
        return new PageDto<>(gameFlowDtoList, page.getTotal(), gameFlowFormVo.getPage(), gameFlowFormVo.getLimit());
    }

    @Override
    public List<GameFlowEntity> getGameFlowList(List<Long> gameIdList) {
        if (CollectionUtils.isEmpty(gameIdList)) {
            log.info("游戏主键列表为空");
            return Collections.emptyList();
        }
        LambdaQueryWrapper<GameFlowEntity> gameFlowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        gameFlowLambdaQueryWrapper.in(GameFlowEntity::getGameId, gameIdList);
        // 查询游戏流水
        List<GameFlowEntity> gameFlowList = baseMapper.selectList(gameFlowLambdaQueryWrapper);
        log.info("批量查询游戏流水完成。isEmpty={}", gameFlowList.isEmpty());
        return gameFlowList;
    }
}